package com.example.workbench.controller;

import com.example.workbench.common.api.CommonResult;
import com.example.workbench.common.command.AddTaskCommand;
import com.example.workbench.common.command.DelByIdCommand;
import com.example.workbench.common.command.UpdateTaskCommand;
import com.example.workbench.entity.DbTask;
import com.example.workbench.service.IDbTaskService;
import io.micrometer.common.util.StringUtils;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.json.JsonObject;
import java.util.List;

/**
 * <p>
 * 看板中心表 前端控制器
 * </p>
 *
 * @author 
 * @since 2024-08-03
 */
@RestController
@RequestMapping("/task")
public class DbTaskController {
    @Autowired
    private IDbTaskService taskService;

    @PostMapping("/add")
    public CommonResult addTask(@RequestBody AddTaskCommand command) {
        if (StringUtils.isEmpty(command.getTaskName())) {
            return CommonResult.failed("参数不能为空");
        }
        return taskService.addTask(command);
    }
    @PostMapping("/del")
    public CommonResult delTask(@RequestBody DelByIdCommand command) {
        if (StringUtils.isEmpty(String.valueOf(command.getId()))) {
            return CommonResult.failed("参数不能为空");
        }
        taskService.delTask(command.getId());
        return CommonResult.success("");
    }
    @PostMapping("/update")
    public CommonResult updateTask(@RequestBody UpdateTaskCommand command) {
        if (StringUtils.isEmpty(command.getTaskName()) || StringUtils.isEmpty(String.valueOf(command.getId()))) {
            return CommonResult.failed("参数不能为空");
        }
        return taskService.updateTask(command);
    }
    @GetMapping("/query")
    public CommonResult<List<DbTask>> queryTask() {
        List<DbTask> dbTasks = taskService.queryTask();
        System.out.println(dbTasks);
        return CommonResult.success(dbTasks);
    }
}
